﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    public class CardGroup : Entity
    {
        public List<BattleCard> Cards=new List<BattleCard>();
        public BattlePlayer Owner;
        public CardPlace CardPlace;

        void MoveCardTo(CardGroup cardGroup,BattleCard battleCard,CardPlace cardPlace)
        {
            if (!Cards.Remove(battleCard))
            {
                Log.Warning($"{CardPlace}区域不存在对应卡牌");
                return;
            }
            cardGroup.Cards.Add(battleCard);
            battleCard.CardPlace = cardPlace;
            battleCard.CardGroup = cardGroup;
        }

        public async void MoveCards(CardGroup AimGroup, List<BattleCard> cards)
        {
            MoveInfo moveInfo = new MoveInfo
            {
                Cards = cards,
                StartPlace = this.CardPlace,
                EndPlace = AimGroup.CardPlace,
                StartGroup = this,
                EndGroup = AimGroup,
            };
            await BattleEventWatcherComponent.Single.RunEvent(BattleEventType.CardMove, FightModel.Single, moveInfo);
            foreach (var card in cards)
            {
                MoveCardTo(AimGroup, card, moveInfo.EndPlace);
            }
        }

        public void Shuffle()
        {
            Random r = new Random((int)DateTime.Now.Ticks);
            Cards.Sort((x, y) => r.Next(-1, 1));
        }
    }
}
